package com.paypalpay.turntointerfaceservice;

import com.paypalpay.pojo.Constant;
import com.paypalpay.pojo.request.RequestBody.QueryOrderRequestBody;
import com.paypalpay.pojo.request.RequestHeader;
import com.paypalpay.pojo.request.subRequest.QueryOrderRequest;
import com.paypalpay.pojo.response.responseBody.QueryOrderResponseBody;
import com.paypalpay.pojo.response.subResponse.QueryOrderResponse;
import com.paypalpay.service.PartnerService;
import com.paypalpay.turntointerfaceservice.utils.TurntoInterfaceUtils;
import com.paypalpay.util.JaxbUtil;
import com.paypalpay.util.wx.HttpClientTool;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Demo 去北京查询当前设备的订单
 *
 * @author lm
 * @date 2018/08/31
 */
public class TrunToQueryorderService {
    @Autowired
    PartnerService partnerService;

    public QueryOrderResponseBody queryOrder(String partnerCode, String deviceId, String deviceType,String secret) {
        try {
            QueryOrderRequestBody body = new QueryOrderRequestBody();
            body.setDeviceId(deviceId);
            body.setDeviceType(deviceType);
            // 请求参数拼接
            QueryOrderRequest request = new QueryOrderRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_queryOrder, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(QueryOrderRequest.class).toXml(request, "utf-8");
            System.out.println("请求xml报文：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            System.out.println("响应报文：" + result);
            QueryOrderResponse responseObj = new JaxbUtil(QueryOrderResponse.class).fromXml(result, "utf-8");

            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();
            System.out.println("获取到了" + retCode);

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                //成功，拿到回传的结果回传前端去,只有body才包括所有packageOrder
                // List<PackageOrder> orderList=responseObj.getBody().getPackageOrder();
                QueryOrderResponseBody re = responseObj.getBody();
                return re;
            } else {
                QueryOrderResponseBody re = responseObj.getBody();
                return re;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

